//
//  XKLFileManager.m
//  GuiZiShouDuanZi
//
//  Created by xmg on 16/5/3.
//  Copyright © 2016年 xiao_kl. All rights reserved.
//

#import "XKLFileManager.h"

@implementation XKLFileManager

+(void)fileManagerWithPath:(NSString *)path{
    NSFileManager *mgr=[NSFileManager defaultManager];
    
    [self exceptonWithPath:path];
    
    //获取子文件
    NSArray *subFiles=[mgr contentsOfDirectoryAtPath:path error:nil];
    
    for (NSString *subFile in subFiles) {
        //拼接路径
        NSString *subPath=[path stringByAppendingPathComponent:subFile];
        
        //删除文件
        [mgr removeItemAtPath:subPath error:nil];
    }
}

+(NSInteger)getSizeFileManagerWithPath:(NSString *)path{
    
    NSFileManager *mgr=[NSFileManager defaultManager];
    
    [self exceptonWithPath:path];
    NSInteger size=0;
    
    //获取子文件夹
    NSArray *subpaths=[mgr subpathsAtPath:path];
    for (NSString *file in subpaths) {
        //拼接路径获取子文件路劲
        NSString *subPath=[path stringByAppendingPathComponent:file];
        
        //如果是隐藏文件或者文件夹不需要计算
        if([subPath containsString:@".DS"]) continue;
        
        BOOL isDirectory=NO;
        BOOL isExist=[mgr fileExistsAtPath:subPath isDirectory:&isDirectory];
        if (!isDirectory||!isExist) continue;
        
        //计算大小
        NSInteger fileSize=[[mgr attributesOfItemAtPath:subPath error:nil] fileSize];
        size+=fileSize;
    }
    NSLog(@"%@",path);
    return size;
}

+(void)exceptonWithPath:(NSString *)path{

     NSFileManager *mgr=[NSFileManager defaultManager];
    //判断穿的是否是路径
    BOOL isDirectory=NO;
    //判断文件是否存在
    BOOL isexist=[mgr fileExistsAtPath:path isDirectory:&isDirectory];

    if (!isDirectory||!isexist) {
        //异常对象
        NSException *excepton=[NSException exceptionWithName:@"路径名错误" reason:@"笨蛋，传错了路径，只能传文件夹路径，并且要存在" userInfo:nil];
        //开启异常
        [excepton raise];
        
    }
}
@end
